package cn.iocoder.yudao.module.handguard.controller.admin.zoneprice;

import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.zoneprice.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.zoneprice.ZonePriceDO;
import cn.iocoder.yudao.module.handguard.convert.zoneprice.ZonePriceConvert;
import cn.iocoder.yudao.module.handguard.service.zoneprice.ZonePriceService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 区域-小区-评估价记录")
@RestController
@RequestMapping("/hutai/zone-price")
@Validated
public class ZonePriceController {

    @Resource
    private ZonePriceService zonePriceService;

    @PostMapping("/create")
    @Operation(summary = "创建区域-小区-评估价记录")
    @PreAuthorize("@ss.hasPermission('hutai:zone-price:create')")
    public CommonResult<Long> createZonePrice(@Valid @RequestBody ZonePriceCreateReqVO createReqVO) {
        return success(zonePriceService.createZonePrice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新区域-小区-评估价记录")
    @PreAuthorize("@ss.hasPermission('hutai:zone-price:update')")
    public CommonResult<Boolean> updateZonePrice(@Valid @RequestBody ZonePriceUpdateReqVO updateReqVO) {
        zonePriceService.updateZonePrice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除区域-小区-评估价记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:zone-price:delete')")
    public CommonResult<Boolean> deleteZonePrice(@RequestParam("id") Long id) {
        zonePriceService.deleteZonePrice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得区域-小区-评估价记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:zone-price:query')")
    public CommonResult<ZonePriceRespVO> getZonePrice(@RequestParam("id") Long id) {
        ZonePriceDO zonePrice = zonePriceService.getZonePrice(id);
        return success(ZonePriceConvert.INSTANCE.convert(zonePrice));
    }

    @GetMapping("/list")
    @Operation(summary = "获得区域-小区-评估价记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:zone-price:query')")
    public CommonResult<List<ZonePriceRespVO>> getZonePriceList(@RequestParam("ids") Collection<Long> ids) {
        List<ZonePriceDO> list = zonePriceService.getZonePriceList(ids);
        return success(ZonePriceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得区域-小区-评估价记录分页")
    @PreAuthorize("@ss.hasPermission('hutai:zone-price:query')")
    public CommonResult<PageResult<ZonePriceRespVO>> getZonePricePage(@Valid ZonePricePageReqVO pageVO) {
        PageResult<ZonePriceDO> pageResult = zonePriceService.getZonePricePage(pageVO);
        return success(ZonePriceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出区域-小区-评估价记录 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:zone-price:export')")
    @OperateLog(type = EXPORT)
    public void exportZonePriceExcel(@Valid ZonePriceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ZonePriceDO> list = zonePriceService.getZonePriceList(exportReqVO);
        // 导出 Excel
        List<ZonePriceExcelVO> datas = ZonePriceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "区域-小区-评估价记录.xls", "数据", ZonePriceExcelVO.class, datas);
    }


    @PostMapping("/import")
    @Operation(summary = "导入区域小区记录")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('hutai:zone-price:import')")
    public CommonResult<ZonePriceImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<ZonePriceImportExcelVO> list = ExcelUtils.read(file, ZonePriceImportExcelVO.class);
        return success(zonePriceService.importUserList(list, updateSupport));
    }

}
